<template>
	<view class="">
		<view class="flex" style="">
			<view class="  p2" style="background-color: #fff;width: 100%;">
				<scroll-view scroll-y="true" class="scroll-Y">
					<uni-card>
						<template v-slot:title>
							<view>
								<uni-row>
									<uni-col :xs="10" :md="4">
										<view class="button_space" style="padding-top: 10px;">
											<button @click="on_add_gg(1)" type="primary" size="mini">添加公告</button>
											<button @click="on_add_gg(0)" type="warn" size="mini"
												style="margin-left: 10px;">添加轮播图</button>
										</view>
									</uni-col>
								</uni-row>
							</view>
						</template>
						<uni-table border stripe type="selection" :type="'2'" @selection-change="selectionData">
							<uni-tr>
								<uni-th align="center" width="30px">id</uni-th>
								<uni-th align="center" width="30px">图片</uni-th>
								<uni-th align="center" width="30px">公告标题</uni-th>
								<uni-th align="center" width="30px">类型</uni-th>



								<uni-th align="center" width="130px">管理</uni-th>
							</uni-tr>
							<uni-tr v-for="(vo,index) in list" :key="index">
								<uni-td>
									<view class="flex j-c">{{vo.id}}</view>
								</uni-td>
								<uni-td>
									<view class="flex j-c" v-if="vo.img">
										<image class="b_y" :src="url+vo.img" style="width: 200px;height: 130px;"
											mode="">
										</image>
									</view>
								</uni-td>
								<uni-td>
									<view class="flex j-c">{{vo.name}}</view>
								</uni-td>
								<uni-td>
									<view class="flex j-c">
										{{vo.type==0? '轮播图':'公告'	}}

									</view>
								</uni-td>




								<uni-td>
									<view class="flex j-c">
										<view>

											<view class="">
												<button @click="on_del(index)"
													style="background-color: red;width: 88px;" type="primary"
													size="mini">删除</button>
											</view>
										</view>
									</view>
								</uni-td>
							</uni-tr>
						</uni-table>
						<template v-slot:actions>
							<view style="width: 100%;">
								<uni-pagination @change="getPage" show-icon="true" :total="total" current="1"
									style="float: left;margin-bottom: 10px;"></uni-pagination>
							</view>
						</template>
					</uni-card>
				</scroll-view>
			</view>
		</view>

		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-beizhu cancelText="取消" confirmText="确认" lei="0" :name="name" :keya="keya" @confirm="onxiugai">
			</uni-popup-beizhu>
		</uni-popup>

		<uni-popup ref="alertDialog_img" type="dialog">
			<view class="bg-white p3 b_y" style="width: 500px;margin-top: -300px;">
				<view class="p2 flex j-c w800">
					<text class="f30">添加公告</text>
				</view>
				<view class="flex a-c" v-if="type==1">
					<view class="flex a-c  pb3" style="width: 500px;">
						<view class="f28 c_7" style="width: 110px;">
							公告内容
						</view>
						<uni-easyinput v-model="find.name" placeholder="请输入内容">
						</uni-easyinput>
					</view>
				</view>
				<view class="flex a-c" v-if="type==0">
					<view class=" flex j-b pb3" style="width: 340px;">
						<view class="  ">
							<view class="pb2 c_7 f18  ">
								首页顶部轮播图
							</view>
							<image :src="url+find.home_img" style="width: 230px;height: 120px;border: solid 2rpx #c0c0c0;" mode=""></image> 
						</view>
						<view class="" style="padding-top: 130px;">
							<button class=" " @click="on_img()" type="primary" size="mini">选择图片</button>

						</view>


					</view>
				</view>

				<view class="  p2 flex j-c">
					<button class=" " @click="on_ajax_name()" type="primary" style="width: 200px;"
						size="mini">添加</button>
				</view>
			</view>
		</uni-popup>


	</view>
</template>

<script>
		import $http from '@/common/requestConfig';
	export default {
		components: {},
		data() {
			return {
				list: [],
				last_page: 0,
				total: 0,
				page: 1,
				name: '',
				keya: '',
				uid: '',
				tel: '',
				find: {
					home_img:'',
					name:''
				},
				type: 0,
				url:''
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			on_img() {
			
				var that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: res => {
						const tempFilePaths = res.tempFilePaths;
						that._Upload(tempFilePaths[0])
			
					}
				});
			},
			_Upload(path) {
				uni.showLoading({
					title: '正在上传中...'
				})
			
				var that = this;
				var img_list = new Array();
				var map = {
					path: path,
				};
				img_list.push(map);
				$http.qnFileUpload({
					files: img_list, // 必填 临时文件路径 格式: [{path: "图片地址"}]
					load: true,
					maxSize: 300000,
					typeo: '',
					onEachUpdate: res => {
			
					},
					onProgressUpdate: res => {
			
			
					}
				}).then(res => {
			
					this.url = res[0]['homg_oss_url']
					if (res[0]['homg_oss'] == 0) {
						this.url = res[0]['homg_url']
					}
					uni.hideLoading();
					that.find['home_img'] = res[0]['url'];
			 
			
			
			
			
				});
			},
			on_add_gg(type) {
				this.$refs.alertDialog_img.open()
				this.type = type
			},
			on_ajax_name() {
				var that = this;
				var img =  that.find['home_img'];
				var name = that.find['name'];
				var type = this.type;
				let requestParams = {
					img: img,
					name: name,
					type: type,
				};
				this.Htpp.post('banmi_tongcheng.Homenav/adds_tjgghlb', 'app', {
					token: false,
					datalist: requestParams
				}).then(ret => {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none',
					})
					if (ret.data.status == 200) {
						this.url = ret.data.oss_url
						if (ret.data.oss == 0) {
							this.url = ret.data.url
						}
						this.init()
						this.$refs.alertDialog_img.close()
					}
				})
			},

			on_refuse(index) {
				this.id = this.list[index]['id'];
				this.index = index;
				this.name = '请输入拒绝的理由';
				this.keya = 'aa';
				this.$refs.alertDialog.open()
			},
			onxiugai(val) {
				var that = this;
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					refuse_text: val,
					id: this.id,
					status: 0,
				};
				this.Htpp.post('banmi_tongcheng.Homenav/updates_refuse', 'app', {
					token: false,
					datalist: requestParams
				}).then(ret => {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					if (ret.data.status == 200) {
						this.list[that.index]['status'] = 0;
						that.$refs.alertDialog.close()
					}
				})

			},
			getPage(id) {
				this.page = id.current;
				this.init()
			},
			on_del(index) {
				var that = this;
				var id = this.list[index]['id'];
				uni.showModal({
					title: '提示',
					content: '确认删除吗？',
					success: function(res) {
						if (res.confirm) {
							that.ajax_del(id);
						}
					}
				});

			},
			ajax_del(id) {
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					id: id,
				};
				this.Htpp.post('banmi_tongcheng.Homenav/updates_del', 'app', {
					token: false,
					datalist: requestParams
				}).then(ret => {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					if (ret.data.status == 200) {
						that.list = that.list.filter(t => t.id != id);
					}
				})

			},
			ajax_adopt(id, index) {
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					id: id,
				};
				this.Htpp.post('banmi_tongcheng.Homenav/updates_adopt', 'app', {
					token: false,
					datalist: requestParams
				}).then(ret => {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					if (ret.data.status == 200) {
						that.list[index]['status'] = 2;
					}
				})
			},

			on_tongguo(index) {
				var that = this;
				var id = this.list[index]['id'];
				uni.showModal({
					title: '提示',
					content: '确认审核通过吗？',
					success: function(res) {
						if (res.confirm) {
							that.ajax_adopt(id, index);
						}
					}
				});

			},
			on_cha() {
				this.init();
			},
			on_cha_a() {
				this.uid = '';
				this.tel = '';
				this.init();
			},
			init() {
				var that = this;
				let requestParams = {
					user_id: uni.getStorageSync('user_id'),
					token: uni.getStorageSync('token'),
					page: this.page,
					status: this.status,
				};
				this.Htpp.post('banmi_tongcheng.Homenav/paginate_cxsylb', 'app', {
					token: false,
					datalist: requestParams
				}).then(ret => {
					this.url = ret.data.img_url
					 
	 
					this.list = ret.data.data.list.list;
					this.total = ret.data.data.list.total;
				})
			},

			on_tab(index) {
				this.tabindex = index;
			},


		}
	}
</script>

<style>
	page {
		background-color: #F2F5FF;
	}

	.feng {
		color: red;
	}
</style>
